﻿using System;
using FirebirdSql.Data.FirebirdClient;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace RMSM
{
    [Serializable()]
    public class TrangPhucBOCollection : Csla.BusinessListBase<TrangPhucBOCollection, TrangPhucBO>
    {
        public int STTBegin = 0;

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            TrangPhucBO _TrangPhucBO = TrangPhucBO.NewChildTrangPhucBO();
            _TrangPhucBO.STT0 = ++this.STTBegin;
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_TrangPhucBO));

            Add(_TrangPhucBO);
            return _TrangPhucBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private TrangPhucBOCollection()
        {
        }

        public static TrangPhucBOCollection NewTrangPhucBOCollection()
        {
            return new TrangPhucBOCollection();
        }

        public static TrangPhucBO NewTrangPhucBO()
        {
            return TrangPhucBO.NewChildTrangPhucBO();
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection()
        {
            return GetTrangPhucBOCollection(null, null, null, false);
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection(bool getDeleted)
        {
            return GetTrangPhucBOCollection(null, null, null, getDeleted);
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection(string where)
        {
            return GetTrangPhucBOCollection(where, null, null, false);
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection(string where, bool getDeleted)
        {
            return GetTrangPhucBOCollection(where, null, null, getDeleted);
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection(string where, string orderCol)
        {
            return GetTrangPhucBOCollection(where, orderCol, null, false);
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection(string where, string orderCol, bool getDeleted)
        {
            return GetTrangPhucBOCollection(where, orderCol, null, getDeleted);
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection(string where, string orderCol, string orderExp)
        {
            return GetTrangPhucBOCollection(where, orderCol, orderExp, false);
        }

        public static TrangPhucBOCollection GetTrangPhucBOCollection(string where, string orderCol, string orderExp, bool getDeleted)
        {
            string swhere = "";
            if (getDeleted == false)
                swhere += "WHERE DELE='0'" + (string.IsNullOrEmpty(where) ? "" : " AND " + where);
            else
                swhere = where;

            SPParam spParam = new SPParam((string.IsNullOrEmpty(orderCol) ? "TRANGPHUCID" : orderCol), (string.IsNullOrEmpty(orderExp) ? "ASC" : orderExp), swhere);
            return DataPortal.Fetch<TrangPhucBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            Session.hstMauSac = new Hashtable();
            Session.hstXuatXu = new Hashtable();
            Session.hstPhuKien = new Hashtable();

            RaiseListChangedEvents = false;
            using (FbConnection conn = new FbConnection(Session.CnnStr))
            {
                conn.Open();
                using (FbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "TrangPhucBO_C";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@WHEREEXP", FbDbType.VarChar).Value = spParam.whereExpression;
                    cmd.Parameters.Add("@ORDERCOL", FbDbType.VarChar).Value = spParam.orderColumn;
                    cmd.Parameters.Add("@ORDEREXP", FbDbType.VarChar).Value = spParam.orderExpression;

                    using (FbDataReader reader = cmd.ExecuteReader())
                    {
                        string sTmp = "";
                        while (reader.Read())
                        {
                            TrangPhucBO trangPhucBO = TrangPhucBO.GetChildTrangPhucBO(reader);

                            sTmp = trangPhucBO.MauSac;
                            if (sTmp != null)
                            {
                                sTmp = sTmp.ToUpper();
                                if (Session.hstMauSac.ContainsKey(sTmp) == false)
                                    Session.hstMauSac.Add(sTmp, trangPhucBO.MauSac);
                            }

                            sTmp = trangPhucBO.XuatXu;
                            if (sTmp != null)
                            {
                                sTmp = sTmp.ToUpper();
                                if (Session.hstXuatXu.ContainsKey(sTmp) == false)
                                    Session.hstXuatXu.Add(sTmp, trangPhucBO.XuatXu);
                            }

                            sTmp = trangPhucBO.PhuKien;
                            if (sTmp != null)
                            {
                                sTmp = sTmp.ToUpper();
                                if (Session.hstPhuKien.ContainsKey(sTmp) == false)
                                    Session.hstPhuKien.Add(sTmp, trangPhucBO.PhuKien);
                            }

                            this.Add(trangPhucBO);
                        }
                    }
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            TrangPhucBO child = null;
            int length = this.Count;
            bool del = false;
            for (int i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.DELE);

                if (child.IsNew && !del)
                    child.Insert();
                else
                    child.Update();

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
            }

            SortByStt();
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public TrangPhucBO GetBO(int? TrangPhucID)
        {
            if (TrangPhucID == null)
                return null;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].TrangPhucID == TrangPhucID)
                    return this[i];

            return null;
        }

        public int GetIndex(int? TrangPhucID)
        {
            if (TrangPhucID == null)
                return -1;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].TrangPhucID == TrangPhucID)
                    return i;

            return -1;
        }

        public void Remove(int? TrangPhucID)
        {
            if (TrangPhucID == null)
                return;

            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].TrangPhucID == TrangPhucID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }

                    break;
                }
            }
        }

        public void SortByStt()
        {
            SortingByStt(0, this.Count - 1);
        }

        private void SortingByStt(int beg, int end)
        {
            if (end <= beg)
                return;
            else
            {
                int pivot = GetPivotPointByStt(beg, end);
                if (pivot > beg)
                    SortingByStt(beg, pivot - 1);
                if (pivot < end)
                    SortingByStt(pivot + 1, end);
            }
        }

        private string GetCompareValue(TrangPhucBO trangPhucBO)
        {
            string s = "";
            if (trangPhucBO.LoaiTrangPhucID == null)
                s = "000000";
            else
                s = trangPhucBO.LoaiTrangPhucID.Value.ToString("000000");

            s += (string.IsNullOrEmpty(trangPhucBO.Ma) ? "" : trangPhucBO.Ma);
            return s;
        }

        private int GetPivotPointByStt(int begPoint, int endPoint)
        {
            int pivot = begPoint;
            int m = begPoint + 1;
            int n = endPoint;
            string sttPivot = GetCompareValue(this[pivot]);
            string sttM = GetCompareValue(this[m]);
            string sttN = GetCompareValue(this[n]);
            TrangPhucBO temp = null;

            while ((m < endPoint) && sttPivot.CompareTo(sttM) >= 0)
            {
                m++;
                sttM = GetCompareValue(this[m]);
            }

            while ((n > begPoint) && sttPivot.CompareTo(sttN) <= 0)
            {
                n--;
                sttN = GetCompareValue(this[n]);
            }

            while (m < n)
            {
                temp = this[m].Clone();
                this[m] = this[n];
                this[n] = temp;
                sttM = GetCompareValue(this[m]);
                sttN = GetCompareValue(this[n]);

                while (m < endPoint && sttPivot.CompareTo(sttM) >= 0)
                {
                    m++;
                    sttM = GetCompareValue(this[m]);
                }

                while (n > begPoint && sttPivot.CompareTo(sttN) <= 0)
                {
                    n--;
                    sttN = GetCompareValue(this[n]);
                }
            }

            if (pivot != n)
            {
                temp = this[n].Clone();
                this[n] = this[pivot];
                this[pivot] = temp;
            }

            return n;
        }
        #endregion Business Method
        //==========================================================================
    }
}